/*****  Component, by Ben Bi <prince.bi@gmail.com>  created at: 12/23/2009 4:10:45 PM  *****/

/********   Notes:  *******
 *  Two tags need to be replaced with the module name one by one, they are:
 *  weaveOrder
 *  WeaveOrder
 *  weave_order
 */

/***** variables declartion section, begin *****/
var weaveOrderItemInputMode = wacFormInputMode.add;
var weaveOrderItemObj = {};
var weaveOrderObj = {};

var weaveOrderItemDataStore = {items:[]};  // to store the items
var weaveOrderItem_fillingYarnGoodsListCurrentPage = 1;
var weaveOrderItem_fillingYarnGoodsListTotalPages = 1;
var weaveOrderItem_warpYarnGoodsListCurrentPage = 1;
var weaveOrderItem_warpYarnGoodsListTotalPages = 1;

/***** variables declartion section, end *****/

/***** init section, begin *****/
$(document).ready(
    function() {
        initWeaveOrderItemFormDialog();

        initWeaveOrderItemForm();

        bindWeaveOrderItemEvents();
    }
);

function bindWeaveOrderItemEvents()
{
    $("#weaveOrderItemFormDialog").bind('dialogopen', function(event, ui)
    {
        initWeaveOrderItemFormData();
//        wacDebugLog("calling " + moduleName + ":" + wacModelObj.weaveOrderItem.name);//
    });

    $("#weaveOrderItemFormDialog").bind('dialogclose', function(event, ui)
    {
        $.validationEngine.closePrompt(".formError", true);
    });

    $("#weaveOrderItem_btnSubmit").bind('click', function (e)
    {
        submitWeaveOrderItemForm();
    });

    $("#weaveOrderItem_btnClose").bind('click', function (e)
    {
        $("#weaveOrderItemFormDialog").dialog('close');
    });

    $("#weaveOrderItem_goods_number, #weaveOrderItem_goods_number_unit_price").bind('change', function (e)
    {
        $("#weaveOrderItem_processing_fee").attr("value", Number($("#weaveOrderItem_goods_number").val()) * Number($("#weaveOrderItem_goods_number_unit_price").val()));
        $("#weaveOrderItem_processing_fee").formatCurrency({symbol:"",digitGroupSymbol:"", roundToDecimalPlace:-1});
    });

    $("#weaveOrderItem_btnPrevStockFillingYarnGoodsList").bind('click', function (e)
    {
        if(weaveOrderItem_fillingYarnGoodsListCurrentPage>1)
        {
            weaveOrderItem_fillingYarnGoodsListCurrentPage--;
            if($('#weaveOrderItem_weave_order_id').val()!="")
            {
                weaveOrderItemRefleshStockFillingYarnGoodsList(weaveOrderObj.supplier_id);
            }
        }
    });

    $("#weaveOrderItem_btnNextStockFillingYarnGoodsList").bind('click', function (e)
    {
        if(weaveOrderItem_fillingYarnGoodsListCurrentPage < weaveOrderItem_fillingYarnGoodsListTotalPages)
        {
            weaveOrderItem_fillingYarnGoodsListCurrentPage++;
            if($('#weaveOrderItem_weave_order_id').val()!="")
            {
                weaveOrderItemRefleshStockFillingYarnGoodsList(weaveOrderObj.supplier_id);
            }
        }
    });

    $("#weaveOrderItem_btnPrevStockWarpYarnGoodsList").bind('click', function (e)
    {
        if(weaveOrderItem_warpYarnGoodsListCurrentPage>1)
        {
            weaveOrderItem_warpYarnGoodsListCurrentPage--;
            if($('#weaveOrderItem_weave_order_id').val()!="")
            {
                weaveOrderItemRefleshStockWarpYarnGoodsList(weaveOrderObj.supplier_id);
            }
        }
    });

    $("#weaveOrderItem_btnNextStockWarpYarnGoodsList").bind('click', function (e)
    {
        if(weaveOrderItem_warpYarnGoodsListCurrentPage < weaveOrderItem_warpYarnGoodsListTotalPages)
        {
            weaveOrderItem_warpYarnGoodsListCurrentPage++;
            if($('#weaveOrderItem_weave_order_id').val()!="")
            {
                weaveOrderItemRefleshStockWarpYarnGoodsList(weaveOrderObj.supplier_id);
            }
        }
    });

    $("#weaveOrderItem_btnTransferFillingYarnGoods").bind('click', function (e)
    {
        weaveOrderItemTransferConsumeGoods("#weaveOrderItem_stockFillingYarnGoodsList", "#weaveOrderItem_targetFillingYarnGoodsList", "filling_yarn_consume_items");
    });
    


    $("#weaveOrderItem_btnTransferWarpYarnGoods").bind('click', function (e)
    {
        weaveOrderItemTransferConsumeGoods("#weaveOrderItem_stockWarpYarnGoodsList", "#weaveOrderItem_targetWarpYarnGoodsList", "warp_yarn_consume_items");
    });

    // fix dialog div didnt remove bug, remove it by this way
    $('#appStorehouseRightPane').bind('tabsremove', function(event, ui) {
        if(ui.panel.id == "t13003")
        {
            $("div[aria-labelledby='ui-dialog-title-weaveOrderItemFormDialog']").remove();
            $("#weaveOrderItemFormDialog").remove();
        }
    });
}


function initWeaveOrderItemFormDialog()
{
//    wacDebugLog("initWeaveOrderItemFormDialog.");
    $("#weaveOrderItemFormDialog").dialog({
            bgiframe: true,
            modal: true,
            width: 1060,
            height: 720,
            autoOpen: false,
            buttons: {},
            zIndex: 100
        });
}

function initWeaveOrderItemForm()
{
    $("#weaveOrderItemGoodsConsumeTabs").tabs();

    $("#weaveOrderItemForm").validationEngine();

    $('.wacFormContent .left, .wacFormContent input, .wacFormContent textarea, .wacFormContent select').focus(function(){
        $(this).parents('.wacFormContent').addClass("wacFormOver");
    }).blur(function(){
        $(this).parents('.wacFormContent').removeClass("wacFormOver");
    });

    $('.wacFormContentA .left, .wacFormContentA input, .wacFormContentA textarea, .wacFormContentA select').focus(function(){
        $(this).parents('.wacFormRow').addClass("wacFormOver");
    }).blur(function(){
        $(this).parents('.wacFormRow').removeClass("wacFormOver");
    });

    $("#weaveOrderItem_stockFillingYarnGoodsList").selectable();
    $("#weaveOrderItem_stockWarpYarnGoodsList").selectable();

    // init the good list css and selectable
    $('ol#weaveOrderItem_stockFillingYarnGoodsList li, ol#weaveOrderItem_stockWarpYarnGoodsList li').hover(
        function() {
            $(this).addClass('ui-state-hover');
        },
        function() {
            $(this).removeClass('ui-state-hover');
        }
    );


}

function initWeaveOrderItemFormData()
{
    wacShowBlockUILoading("#weaveOrderItemFormDialog");
    $('#weaveOrderItem_spinner_id, #weaveOrderItem_cotton_goods_id, #weaveOrderItem_warp_yarn_goods_id, #weaveOrderItem_color_id, #weaveOrderItem_goods_id, #weaveOrderItem_goods_number_unit_code, #weaveOrderItem_filling_yarn_arrangement_id, #weaveOrderItem_warp_yarn_arrangement_id').empty();

    var params = {
        "data_format" : 'json',
        "weave_order_id": weaveOrderObj.id
    };
    
    if(weaveOrderItemInputMode == wacFormInputMode.add)
    {
        params.id = 0;
    }
    else
    {
        params.id = weaveOrderItemObj.id;
    }

    $.ajax({
        url: BASE_URL + "weaveOrderItem/getFormData",
        global: true,
        type: "GET",
        data: params,
        dataType: "json",
        success: function(jsonData){
            initWeaveOrderItemFormDataCallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            wacDebugLog("getFormData Error: " + $(this).dump()); // the options for this ajax request
        }
    });
}

function initWeaveOrderItemFormDataCallBack(jsonData)
{
   if(jsonData.info.status == wacOperationStatus.Error)
   {
       alert(jsonData.info.error_info);
   }
   else
    {
        var selectedIndex = 0;

        for(i=0;i<jsonData['items']['spinner'].length;i++)
        {
            $('<option value="' + jsonData['items']['spinner'][i].key +'">' + jsonData['items']['spinner'][i].value +'</option>').appendTo("#weaveOrderItem_spinner_id");
        }

        for(i=0;i<jsonData['items']['warp_yarn_arrangement'].length;i++)
        {
            $('<option value="' + jsonData['items']['warp_yarn_arrangement'][i].key +'">' + jsonData['items']['warp_yarn_arrangement'][i].value +'</option>').appendTo("#weaveOrderItem_warp_yarn_arrangement_id");
        }

        for(i=0;i<jsonData['items']['filling_yarn_arrangement'].length;i++)
        {
            $('<option value="' + jsonData['items']['filling_yarn_arrangement'][i].key +'">' + jsonData['items']['filling_yarn_arrangement'][i].value +'</option>').appendTo("#weaveOrderItem_filling_yarn_arrangement_id");
        }

        for(i=0;i<jsonData['items']['unit'].length;i++)
        {
            $('<option value="' + jsonData['items']['unit'][i].key +'">' + jsonData['items']['unit'][i].value +'</option>').appendTo("#weaveOrderItem_goods_number_unit_code");
        }

        var itemStr = "";
        weaveOrderItemEmptyStockFillingYarnGoodsList();
        if(jsonData['items']['stock_filling_yarn_items']['items']!=undefined){
            weaveOrderItem_fillingYarnGoodsListTotalPages = jsonData['items']['stock_filling_yarn_items']['totalPages'];
            if(weaveOrderItem_fillingYarnGoodsListTotalPages > 1){
                $("#weaveOrderItem_stockFillingYarnGoodsListPageBtns").show();
            }
            for(i=0;i<jsonData['items']['stock_filling_yarn_items']['items'].length;i++)
            {
                itemStr = weaveOrderItemSetupStockGoodsItemStr(jsonData['items']['stock_filling_yarn_items']['items'][i]);
                $(itemStr).appendTo("#weaveOrderItem_stockFillingYarnGoodsList");
            }
        }

        weaveOrderItemEmptyTargetFillingYarnGoodsList();
        if(jsonData['items']['target_filling_yarn_items']['items']!=undefined){
            for(i=0;i<jsonData['items']['target_filling_yarn_items']['items'].length;i++)
            {
                $(weaveOrderItemDataStore).data(jsonData['items']['target_filling_yarn_items']['items'][i].goods_id, jsonData['items']['target_filling_yarn_items']['items'][i].goods_id);

                itemStr = weaveOrderItemSetupTargetGoodsItemStr(jsonData['items']['target_filling_yarn_items']['items'][i], "filling_yarn_consume_items", false);
                $(itemStr).appendTo("#weaveOrderItem_targetFillingYarnGoodsList");
            }
        }

        weaveOrderItemEmptyStockWarpYarnGoodsList();
        if(jsonData['items']['stock_warp_yarn_items']['items']!=undefined){
            weaveOrderItem_warpYarnGoodsListTotalPages = jsonData['items']['stock_warp_yarn_items']['totalPages'];
            if(weaveOrderItem_warpYarnGoodsListTotalPages > 1){
                $("#weaveOrderItem_stockWarpYarnGoodsListPageBtns").show();
            }
            for(i=0;i<jsonData['items']['stock_warp_yarn_items']['items'].length;i++)
            {
                itemStr = weaveOrderItemSetupStockGoodsItemStr(jsonData['items']['stock_warp_yarn_items']['items'][i]);
                $(itemStr).appendTo("#weaveOrderItem_stockWarpYarnGoodsList");
            }
        }

        weaveOrderItemEmptyTargetWarpYarnGoodsList();
        if(jsonData['items']['target_warp_yarn_items']['items']!=undefined){
            for(i=0;i<jsonData['items']['target_warp_yarn_items']['items'].length;i++)
            {
                $(weaveOrderItemDataStore).data(jsonData['items']['target_warp_yarn_items']['items'][i].goods_id, jsonData['items']['target_warp_yarn_items']['items'][i].goods_id);

                itemStr = weaveOrderItemSetupTargetGoodsItemStr(jsonData['items']['target_warp_yarn_items']['items'][i], "warp_yarn_consume_items", false);
                $(itemStr).appendTo("#weaveOrderItem_targetWarpYarnGoodsList");
            }
        }
   }

   setupWeaveOrderItemFormDefaultValues(jsonData['items']['default']);

   wacHideBlockUI("#weaveOrderItemFormDialog");

//   wacDebugLog($.dump(jsonData));
}

function setupWeaveOrderItemFormDefaultValues(defaultValueObj)
{
    $("#weaveOrderItem_weave_order_id").attr("value", weaveOrderObj.id);
    $("#weaveOrderItem_weave_order_code").attr("innerHTML", weaveOrderObj.code);
    $("#weaveOrderItem_production_order_code").attr("innerHTML", weaveOrderObj.production_order_code);
    $("#weaveOrderItem_supplier_name").attr("innerHTML", weaveOrderObj.supplier_name);
    $("#weaveOrderItem_factory_name").attr("innerHTML", weaveOrderObj.factory_name);

    if(weaveOrderItemInputMode == wacFormInputMode.add)   // use default values
    {
        $("#weaveOrderItem_id").attr("value", 0);
        $('#weaveOrderItem_goods_id').flexbox(BASE_URL + 'goods/GetWeaveHashInFormat?data_format=jsonFlexbox', {
            //            resultTemplate: '<div class="col1">{id}</div><div class="col2">{name}</div>',
            watermark: '请输入选择...',
            noResultsText : '没找到相符项!',
            width: 200,
            maxVisibleRows: 15,
            autoCompleteFirstMatch: true,
            queryDelay: 200,
            minChars: 1,
            paramName: "goods_id",
            inputName: "goods_name",
            onSelect: function() {
//                wacDebugLog('You selected "' + this.value + '", ' +
//                    'which has a hidden value of ' + this.getAttribute('hiddenValue'));
            }
        });
        
        $('#weaveOrderItem_color_id').flexbox(BASE_URL + 'color/GetIdNameHashInFormat?data_format=jsonFlexbox', {
            //            resultTemplate: '<div class="col1">{id}</div><div class="col2">{name}</div>',
            watermark: '请输入选择...',
            noResultsText : '没找到相符项!',
            width: 200,
            maxVisibleRows: 15,
            autoCompleteFirstMatch: true,
            queryDelay: 200,
            minChars: 1,
            paramName: "color_id",
            inputName: "color_name",
            onSelect: function() {
//                wacDebugLog('You selected "' + this.value + '", ' +
//                    'which has a hidden value of ' + this.getAttribute('hiddenValue'));
            }
        });

        $("#weaveOrderItem_goods_number_unit_code option:[value='"+ weaveOrderObj.goods_amount_unit_code +"']").attr('selected', 'selected');
        $("#weaveOrderItem_spinner_id option:[value='"+ defaultValueObj.spinner_id +"']").attr('selected', 'selected');
    }
    else  // use edit obj values
    {
        $('#weaveOrderItem_goods_id').flexbox(BASE_URL + 'goods/GetWeaveHashInFormat?data_format=jsonFlexbox', {
            initialValue: weaveOrderItemObj.goods_id,
            watermark: '请输入选择...',
            noResultsText : '没找到相符项!',
            width: 200,
            maxVisibleRows: 15,
            autoCompleteFirstMatch: true,
            queryDelay: 200,
            minChars: 1,
            paramName: "goods_id",
            inputName: "goods_name",
            onSelect: function() {
//                wacDebugLog('You selected "' + this.value + '", ' +
//                    'which has a hidden value of ' + this.getAttribute('hiddenValue'));
            }
        });
        $('#weaveOrderItem_goods_id_input').val(weaveOrderItemObj.goods_name);


        $('#weaveOrderItem_color_id').flexbox(BASE_URL + 'color/GetIdNameHashInFormat?data_format=jsonFlexbox', {
            initialValue: weaveOrderItemObj.color_id,
            watermark: '请输入选择...',
            noResultsText : '没找到相符项!',
            width: 200,
            maxVisibleRows: 15,
            autoCompleteFirstMatch: true,
            queryDelay: 200,
            minChars: 1,
            paramName: "color_id",
            inputName: "color_name",
            onSelect: function() {
//                wacDebugLog('You selected "' + this.value + '", ' +
//                    'which has a hidden value of ' + this.getAttribute('hiddenValue'));
            }
        });
        $('#weaveOrderItem_color_id_input').val(weaveOrderItemObj.color_name);

//        $("#weaveOrderItem_batch").attr("value", weaveOrderItemObj.batch);
        $("#weaveOrderItem_goods_number_unit_price").attr("value", weaveOrderItemObj.goods_number_unit_price);
        $("#weaveOrderItem_goods_number").attr("value", weaveOrderItemObj.goods_number);
        $("#weaveOrderItem_id").attr("value", weaveOrderItemObj.id);
        $("#weaveOrderItem_memo").attr("value", weaveOrderItemObj.memo);

        $("#weaveOrderItem_quota_amount").attr("value", weaveOrderItemObj.quota_amount);
        $("#weaveOrderItem_processing_fee").attr("value", weaveOrderItemObj.processing_fee);
        $("#weaveOrderItem_goods_quantity").attr("value", weaveOrderItemObj.goods_quantity);
        $("#weaveOrderItem_reed_space").attr("value", weaveOrderItemObj.reed_space);
        $("#weaveOrderItem_reed_code").attr("value", weaveOrderItemObj.reed_code);
        $("#weaveOrderItem_piece_length").attr("value", weaveOrderItemObj.piece_length);
        $("#weaveOrderItem_effect_requirement").attr("value", weaveOrderItemObj.effect_requirement);
        $("#weaveOrderItem_warp_density").attr("value", weaveOrderItemObj.warp_density);
        $("#weaveOrderItem_filling_density").attr("value", weaveOrderItemObj.filling_density);
        $("#weaveOrderItem_texture").attr("value", weaveOrderItemObj.texture);
        $("#weaveOrderItem_memo").attr("value", weaveOrderItemObj.memo);
        $("#weaveOrderItem_goods_number_unit_code option:[value='"+ weaveOrderItemObj.goods_number_unit_code +"']").attr('selected', 'selected');
        $("#weaveOrderItem_spinner_id option:[value='"+ weaveOrderItemObj.spinner_id +"']").attr('selected', 'selected');
        $("#weaveOrderItem_filling_yarn_arrangement_id option:[value='"+ weaveOrderItemObj.filling_yarn_arrangement_id +"']").attr('selected', 'selected');
        $("#weaveOrderItem_warp_yarn_arrangement_id option:[value='"+ weaveOrderItemObj.warp_yarn_arrangement_id +"']").attr('selected', 'selected');
    }

//wacDebugLog($.dump(weaveOrderItemObj));
//    wacDebugLog($.dump(weaveOrderObj));
//    wacDebugLog($.dump(defaultValueObj));
//    wacDebugLog("weaveOrderItemInputMode: " + weaveOrderItemInputMode);

}
/***** init section, end *****/


/**************  interaction section, begin  ***************/

function weaveOrderItemOpenModuleForm(moduleFormName, moduleName, inputMode, rowid, parentId)
{
    weaveOrderItemInputMode = inputMode;

    $(weaveOrderItemDataStore).removeData();  // empty data store

    if(parentId === undefined || parentId==0){
        weaveOrderObj = {};
    }
    else
    {
        weaveOrderObj = $("#weaveOrderList").getRowData(parentId);
    }

    if(rowid === undefined || rowid==0){
        weaveOrderItemObj = {};
    }
    else
    {
        weaveOrderItemObj = $("#weaveOrderList_" + weaveOrderObj.id + "_t").getRowData(rowid);
    }

    $('#'+moduleFormName).dialog('open');
}

function validateWeaveOrderItemForm()
{
    var validateFlag = true;

    if (validateFlag && !$("#weaveOrderItemForm").validationEngine({returnIsValid:true}))
    {
        validateFlag = false;
        return validateFlag;
    }

    if(validateFlag && $("#weaveOrderItem_targetFillingYarnGoodsList li").length == 0)
    {
        wacShowTips("请输入消耗的纬纱项目!");
        validateFlag = false;
    }

    if(validateFlag && $("#weaveOrderItem_targetWarpYarnGoodsList li").length == 0)
    {
        wacShowTips("请输入消耗的经纱项目!");
        validateFlag = false;
    }

    return validateFlag;
}

function submitWeaveOrderItemForm()
{
    if(!validateWeaveOrderItemForm()){
        return;
    }

    wacShowBlockUILoading("#weaveOrderItemFormDialog", "处理中...");

    var extraParams = "data_format=json";
    var submitUrl;

    if(weaveOrderItemInputMode == wacFormInputMode.add){
        submitUrl = BASE_URL + "weaveOrderItem/add";
    }
    else{
        submitUrl = BASE_URL + "weaveOrderItem/edit";
    }


    $.ajax({
        url: submitUrl,
//        url: BASE_URL + "test/ajaxTest" ,
        global: true,
        type: "GET",
        data: $("#weaveOrderItemForm").serialize() + "&" + extraParams,
        dataType: "json",
        success: function(jsonData){
            submitWeaveOrderItemFormCallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            wacDebugLog("getFormData Error: " + $(this).dump()); // the options for this ajax request
        }
    });
}

function submitWeaveOrderItemFormCallBack(jsonData)
{
   if(jsonData.info.status == wacOperationStatus.Error)
   {
       wacShowTips(jsonData.info.error_info);
   }
   else
   {
       $("#weaveOrderItemFormDialog").dialog('close');
       wacShowTips(jsonData.info.tips);
       $("#weaveOrderList_" + weaveOrderObj.id + "_t").trigger("reloadGrid");
   }

   wacHideBlockUI("#weaveOrderItemFormDialog");

//   wacDebugLog($.dump(jsonData));
}

function weaveOrderItemTransferConsumeGoods(srcGoodsListId, targetGoodsListId, varName)
{
   $(".ui-selected", $(srcGoodsListId)).each(function(){
       if($(weaveOrderItemDataStore).data($(this).attr("goods_id")) === undefined)
       {
           $(weaveOrderItemDataStore).data($(this).attr("goods_id"), $(this).attr("goods_id"));
            var itemStr = weaveOrderItemSetupTargetGoodsItemStr(this, varName, true);
            $(itemStr).appendTo(targetGoodsListId);
       }
   });
}


function weaveOrderItemSetupTargetGoodsItemStr(objItem, varName, isStock)
{
   if(isStock){   // obj come from stock
       var tmpStr = '<li class="ui-widget-content wacCursor">' + $(objItem).attr("goods_name") ;
           tmpStr += '>&nbsp;消耗量:&nbsp;<input type="text" id="weaveOrderItem_id_' + $(objItem).attr("goods_id") +'_consume_amount" name="'+ varName +'[id_' + $(objItem).attr("goods_id") + '][consume_amount]" value="' + $(objItem).attr("goods_number") + '" maxNumber="' + $(objItem).attr("goods_number") + '" class="validate[required,custom[onlyNumber]] wacFormText" style="width: 45px;">';
           tmpStr += '&nbsp;每码用量:&nbsp;<input type="text" id="weaveOrderItem_id_' + $(objItem).attr("goods_id") +'_unit_consume" name="'+ varName +'[id_' + $(objItem).attr("goods_id") + '][unit_consume]" value="0" class="validate[required,custom[onlyNumber]] wacFormText" style="width: 30px;">';
           tmpStr += '&nbsp;每码应用量:&nbsp;<input type="text" id="weaveOrderItem_id_' + $(objItem).attr("goods_id") +'_expect_consume" name="'+ varName +'[id_' + $(objItem).attr("goods_id") + '][expect_consume]" value="0" class="validate[required,custom[onlyNumber]] wacFormText" style="width: 30px;">';
           tmpStr += '&nbsp;损耗率:&nbsp;<input type="text" id="weaveOrderItem_id_' + $(objItem).attr("goods_id") +'_attrition_rate" name="'+ varName +'[id_' + $(objItem).attr("goods_id") + '][attrition_rate]" value="0" class="validate[required,custom[onlyDigit]] wacFormText" style="width: 30px;">%';
           tmpStr += '<span class="ui-icon ui-icon-circle-minus" style="float:right" onclick="javascript: weaveOrderItemRemoveItem(this, '+ $(objItem).attr("goods_id") +');"></span>';
           tmpStr += '</li>';
   }
   else{  // come from consume
       var tmpStr = '<li class="ui-widget-content wacCursor">' + $(objItem).attr("goods_name") ;
           tmpStr += '>&nbsp;消耗量:&nbsp;<input type="text" id="weaveOrderItem_id_' + $(objItem).attr("goods_id") +'_consume_amount" name="'+ varName +'[id_' + $(objItem).attr("goods_id") + '][consume_amount]" value="' + $(objItem).attr("cotton_yarn_consume_amount") + '" maxNumber="' + $(objItem).attr("cotton_yarn_consume_amount") + '" class="validate[required,custom[onlyNumber]] wacFormText" style="width: 45px;">';
           tmpStr += '&nbsp;每码用量:&nbsp;<input type="text" id="weaveOrderItem_id_' + $(objItem).attr("goods_id") +'_unit_consume" name="'+ varName +'[id_' + $(objItem).attr("goods_id") + '][unit_consume]" value="' + $(objItem).attr("cotton_yarn_unit_consume") + '" class="validate[required,custom[onlyNumber]] wacFormText" style="width: 30px;">';
           tmpStr += '&nbsp;每码应用量:&nbsp;<input type="text" id="weaveOrderItem_id_' + $(objItem).attr("goods_id") +'_expect_consume" name="'+ varName +'[id_' + $(objItem).attr("goods_id") + '][expect_consume]" value="' + $(objItem).attr("cotton_yarn_expect_consume") + '" class="validate[required,custom[onlyNumber]] wacFormText" style="width: 30px;">';
           tmpStr += '&nbsp;损耗率:&nbsp;<input type="text" id="weaveOrderItem_id_' + $(objItem).attr("goods_id") +'_attrition_rate" name="'+ varName +'[id_' + $(objItem).attr("goods_id") + '][attrition_rate]" value="' + $(objItem).attr("attrition_rate") + '" class="validate[required,custom[onlyDigit]] wacFormText" style="width: 40px;">%';
           tmpStr += '<span class="ui-icon ui-icon-circle-minus" style="float:right" onclick="javascript: weaveOrderItemRemoveItem(this, '+ $(objItem).attr("goods_id") +');"></span>';
           tmpStr += '</li>'; 
   }
    return tmpStr;
}

function weaveOrderItemRemoveItem(tagObj, goodId)
{
    $(tagObj).parent().remove();
    $(weaveOrderItemDataStore).removeData(goodId);
}

function weaveOrderItemSetupStockGoodsItemStr(objItem)
{
    var tmpStr = '<li class="ui-widget-content wacCursor" goods_id="' + $(objItem).attr("goods_id") +'" goods_number="' + $(objItem).attr("goods_number")+' "';
        tmpStr += 'goods_name="' + $(objItem).attr("goods_name") + ' " goods_number_unit_code_name="' + $(objItem).attr("goods_number_unit_code_name") +'" >';
        tmpStr += $(objItem).attr("goods_name") + "&nbsp;" + $(objItem).attr("goods_number") + "&nbsp;" + $(objItem).attr("goods_number_unit_code_name") +'</li>';
    return tmpStr;
}

function weaveOrderItemEmptyStockFillingYarnGoodsList()
{
    $("#weaveOrderItem_stockFillingYarnGoodsZone li").remove();
    $("#weaveOrderItem_stockFillingYarnGoodsListPageBtns").hide();
}

function weaveOrderItemEmptyTargetFillingYarnGoodsList()
{
    $("#weaveOrderItem_targetFillingYarnGoodsZone li").remove();
}

function weaveOrderItemEmptyStockWarpYarnGoodsList()
{
    $("#weaveOrderItem_stockWarpYarnGoodsZone li").remove();
    $("#weaveOrderItem_stockWarpYarnGoodsListPageBtns").hide();
}

function weaveOrderItemEmptyTargetWarpYarnGoodsList()
{
    $("#weaveOrderItem_targetWarpYarnGoodsZone li").remove();
}

function weaveOrderItemRefleshStockFillingYarnGoodsList(id)
{
    wacShowBlockUILoading("#weaveOrderItem_stockFillingYarnGoodsZone", "装载中...");

    var submitUrl;
    var params = {
        "data_format" : 'json',
        "factory_id" : id,
        "rows" :5,
        "page" : weaveOrderItem_fillingYarnGoodsListCurrentPage
    };

    submitUrl = BASE_URL + "factory/GetStockFillingYarnGoodsList";
//    submitUrl = BASE_URL + "test/ajaxTest";
    $.ajax({
        url: submitUrl,
        global: true,
        type: "GET",
        data: params,
        dataType: "json",
        success: function(jsonData){
            weaveOrderItemRefleshStockFillingYarnGoodsListCallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            wacDebugLog("getFormData Error: " + $(this).dump()); // the options for this ajax request
        }
    });
}

function weaveOrderItemRefleshStockFillingYarnGoodsListCallBack(jsonData)
{
    if(jsonData.info.status == wacOperationStatus.Error)
    {
        wacShowTips(jsonData.info.error_info);
    }
    else
    {
        weaveOrderItemEmptyStockFillingYarnGoodsList();
        weaveOrderItem_fillingYarnGoodsListTotalPages = jsonData.totalPages;
        if(weaveOrderItem_fillingYarnGoodsListTotalPages > 1){
            $("#weaveOrderItem_stockFillingYarnGoodsListPageBtns").show();
        }
        for(i=0;i<jsonData['items'].length;i++)
        {
            var itemStr = weaveOrderItemSetupStockGoodsItemStr(jsonData['items'][i]);
            $(itemStr).appendTo("#weaveOrderItem_stockFillingYarnGoodsList");
        }
//        wacShowTips(jsonData.info.tips);
    }

    wacHideBlockUI("#weaveOrderItem_stockFillingYarnGoodsZone");
}

function weaveOrderItemRefleshStockWarpYarnGoodsList(id)
{
    wacShowBlockUILoading("#weaveOrderItem_stockWarpYarnGoodsZone", "装载中...");

    var submitUrl;
    var params = {
        "data_format" : 'json',
        "factory_id" : id,
        "rows" :5,
        "page" : weaveOrderItem_warpYarnGoodsListCurrentPage
    };

    submitUrl = BASE_URL + "factory/GetStockWarpYarnGoodsList";
//    submitUrl = BASE_URL + "test/ajaxTest";
    $.ajax({
        url: submitUrl,
        global: true,
        type: "GET",
        data: params,
        dataType: "json",
        success: function(jsonData){
            weaveOrderItemRefleshStockWarpYarnGoodsListCallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            wacDebugLog("getFormData Error: " + $(this).dump()); // the options for this ajax request
        }
    });
}

function weaveOrderItemRefleshStockWarpYarnGoodsListCallBack(jsonData)
{
    if(jsonData.info.status == wacOperationStatus.Error)
    {
        wacShowTips(jsonData.info.error_info);
    }
    else
    {
        weaveOrderItemEmptyStockWarpYarnGoodsList();
        weaveOrderItem_warpYarnGoodsListTotalPages = jsonData.totalPages;
        if(weaveOrderItem_warpYarnGoodsListTotalPages > 1){
            $("#weaveOrderItem_stockWarpYarnGoodsListPageBtns").show();
        }
        for(i=0;i<jsonData['items'].length;i++)
        {
            var itemStr = weaveOrderItemSetupStockGoodsItemStr(jsonData['items'][i]);
            $(itemStr).appendTo("#weaveOrderItem_stockWarpYarnGoodsList");
        }
//        wacShowTips(jsonData.info.tips);
    }

    wacHideBlockUI("#weaveOrderItem_stockWarpYarnGoodsZone");
}
/**************  interaction section, end  ***************/